﻿using System;
using System.Linq;
using Eagles.Application.Model;
using Eagles.Base;
using Eagles.Interface.Core.Scroll;
using Eagles.Interface.DataAccess.Util;
using Eagles.Interface.DataAccess.ScrollAccess;
using Eagles.Application.Model.Scroll.GetScrollImg;
using Eagles.Application.Model.Scroll.GetScrollNews;
using Eagles.Application.Model.Scroll.GetScrollNewsDetail;

namespace Eagles.DomainService.Core.Scroll
{
    public class ScrollHandler : IScrollHandler
    {
        private readonly IScrollAccess iScrollAccess;
        private readonly IUtil util;

        public ScrollHandler(IScrollAccess iScrollAccess, IUtil util)
        {
            this.iScrollAccess = iScrollAccess;
            this.util = util;
        }

        public GetScrollImgResponse GetScrollImg(GetScrollImgRequest request)
        {
            var response = new GetScrollImgResponse();
            if (request.AppId <= 0)
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            if (!util.CheckAppId(request.AppId))
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            var result = iScrollAccess.GetScrollImg(request.AppId, request.PageType);
            if (result == null || result.Count <= 0)
                throw new TransactionException(MessageCode.NoData, MessageKey.NoData);
            response.ImageList = result?.Select(x => new RollImage()
            {
                ImageUrl = x.ImageUrl,
                TargetUrl = x.TargetUrl
            }).ToList();
            return response;
        }

        public GetScrollNewsResponse GetScrollNews(GetScrollNewsRequest request)
        {
            var response = new GetScrollNewsResponse();
            if (request.AppId <= 0)
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            if (!util.CheckAppId(request.AppId))
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            var tokens = util.GetUserId(request.Token, 0);
            var branchId = 0;
            if (tokens != null && tokens.UserId > 0)
                branchId = tokens.BranchId;
            var nowDate = DateTime.Now.ToString("yyyy-MM-dd");
            var date = DateTime.Now.ToString("MM-dd");
            var result = iScrollAccess.GetScrollNews(nowDate, date, request.AppId, branchId);
            if (result != null && result.Any())
            {
                response.SystemNewsList = result.Select(x => new SystemNews()
                {
                    NewsId = x.NewsId,
                    NewsName = x.NewsName,
                    NewsContent = x.NewsContent
                }).ToList();
            }
            else
                throw new TransactionException(MessageCode.NoData, MessageKey.NoData);
            return response;
        }

        public GetScrollNewsDetailResponse GetScrollNewsDetail(GetScrollNewsDetailRequest request)
        {
            var response = new GetScrollNewsDetailResponse();
            if (request.AppId <= 0)
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            if (!util.CheckAppId(request.AppId))
                throw new TransactionException(MessageCode.InvalidParameter, MessageKey.InvalidParameter);
            var result = iScrollAccess.GetScrollNewsDetail(request.NewsId);
            if (result != null)
            {
                response.NewsId = result.NewsId;
                response.NewsName = result.NewsName;
                response.NewsContent = result.NewsContent;
                response.NoticeTime = result.NoticeTime;
            }
            else
                throw new TransactionException(MessageCode.NoData, MessageKey.NoData);
            return response;
        }
    }
}